package com.zprmb.udzt;

import android.app.LocalActivityManager;
import android.app.TabActivity;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.message.PushAgent;
import com.zprmb.udzt.activity.V6NewZhiTouDexterityActivity;
import com.zprmb.udzt.activity.V6ZhitouIndexActivity;
import com.zprmb.udzt.layout.AutoLinearLayout;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.LogUtils;
import com.zprmb.udzt.util.VolleryUtil;
import com.zprmb.udzt.view.StatusBarUtil;

import org.json.JSONObject;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends TabActivity implements OnClickListener, OnTabChangeListener {

    private static TabHost mTabHost;
    private Intent mShouye;
    private Intent mLiebiao;
    private Intent mZichan;
    //	private Intent mZhanghu;
    private Intent mh5Zhanghu;
    private Intent mActive;
    private Intent mDexterity;
    private boolean islogin;
    private final static String TAB_TAG_SHOU = "tab_tag_shouye";
    private final static String TAB_TAG_LIEBIAO = "tab_tag_liebiao";
    private final static String TAB_TAG_Dextrity = "tab_tag_dextrity";
    private final static String TAB_TAG_ZHANGHU = "tab_tag_zhanghu";
    private long mLastBackTime = 0;
    private long TIME_DIFF = 2 * 1000;
    SharedPreferences sha;
    private PushAgent mPushAgent;
    private LinearLayout ll_home_shouye;
    private TextView tv_home_shouye;
    private ImageView iv_home_shouye, iv_home_liebiao, iv_home_dextrity, iv_home_zhanghu, iv_home_shouye_activity;
    private ImageView iv_home_shouye_view, iv_home_liebiao_view, iv_home_dextrity_view, iv_home_zhanghu_view;
    private LinearLayout ll_home_liebiao;
    private TextView tv_home_liebiao;
    private LinearLayout ll_home_dextrity;
    private TextView tv_home_dextrity;
    private LinearLayout ll_home_zhanghu;
    private TextView tv_home_zhanghu;
    private int assets = 0, push = 0, licai;
    private LinearLayout li_activity_view;
    private String page = "";
    private TabHost localTabHost;
    private int textColor_down = Color.parseColor("#C2A166");
    private AutoLinearLayout li_bottom_tab;
    public static MainActivity instance;
//    protected ImmersionBar mImmersionBar;
    @SuppressWarnings("deprecation")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setImmersionStatus();
      /*  mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.init();
        mImmersionBar.statusBarDarkFont(false).init();
        mImmersionBar.flymeOSStatusBarFontColor(R.color.white);*/


        instance = this;
        sha = getSharedPreferences("config", MODE_PRIVATE);
        assets = getIntent().getIntExtra("regist", 0);
        push = getIntent().getIntExtra("tabbarSelectIndex", 0);
        licai = getIntent().getIntExtra("licai", 0);
        mPushAgent = PushAgent.getInstance(this);
        mPushAgent.onAppStart();
        Editor editor = sha.edit();
        StatusBarUtil.StatusBarLightMode(this);
        editor.putString("device_token", mPushAgent.getRegistrationId());
        editor.apply();
        Log.e("create", push + "");
        initView();
//        tab_status(true);
//        userinfo();
//        active();
        Log.e("registrationId", mPushAgent.getRegistrationId() + "333");
//        onClickCopy(mPushAgent.getRegistrationId());
        initData();

    }

    public void onClickCopy(String s) {
        // 从API11开始android推荐使用android.content.ClipboardManager
        // 为了兼容低版本我们这里使用旧版的android.text.ClipboardManager，虽然提示deprecated，但不影响使用。
        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
        cm.setText(s);
        Toast.makeText(this, "复制成功，可以发给朋友们了。", Toast.LENGTH_LONG).show();
    }

    private void tab_status(boolean flag) {
        if (new_year()) {
            deafult();
            li_activity_view.setVisibility(View.VISIBLE);
            textColor_down = Color.RED;
//if(flag){
            li_bottom_tab.setBackgroundColor(Color.parseColor("#facc42"));
//        		iv_home_shouye_view.setVisibility(View.VISIBLE);
//        	tv_home_shouye.setTextColor(Color.RED);
//        		tv_home_shouye.setTextColor(textColor_down);
//        	}

            iv_home_shouye.setBackgroundResource(R.drawable.new_year_one);
            iv_home_liebiao.setBackgroundResource(R.drawable.new_year_three);
            iv_home_dextrity.setBackgroundResource(R.drawable.new_year_two);
            iv_home_zhanghu.setBackgroundResource(R.drawable.new_year_four);
            iv_home_shouye_view.setBackgroundResource(R.drawable.new_year_one_down);
            iv_home_liebiao_view.setBackgroundResource(R.drawable.new_year_three_down);
            iv_home_dextrity_view.setBackgroundResource(R.drawable.new_year_two_down);
            iv_home_zhanghu_view.setBackgroundResource(R.drawable.new_year_four_down);

        } else if (valentine()) {
            deafult();
            li_activity_view.setVisibility(View.VISIBLE);
            iv_home_shouye_view.setBackgroundResource(R.drawable.valentine_one_down);
            iv_home_liebiao_view.setBackgroundResource(R.drawable.valentine_three_down);
            iv_home_dextrity_view.setBackgroundResource(R.drawable.valentine_two_down);
            iv_home_zhanghu_view.setBackgroundResource(R.drawable.valentine_four_down);
//        	li_activity_view.setVisibility(View.GONE);
            textColor_down = Color.parseColor("#C2A166");
            li_bottom_tab.setBackgroundColor(Color.WHITE);
            iv_home_shouye.setBackgroundResource(R.drawable.valentine_one);
            iv_home_liebiao.setBackgroundResource(R.drawable.valentine_three);
            iv_home_dextrity.setBackgroundResource(R.drawable.valentine_two);
            iv_home_zhanghu.setBackgroundResource(R.drawable.valentine_four);
        } else {
            li_bottom_tab.setBackgroundColor(Color.WHITE);
            textColor_down = Color.parseColor("#C2A166");
//        	tv_home_shouye.setTextColor(textCoflor_down);
            li_activity_view.setVisibility(View.GONE);
            iv_home_shouye.setBackgroundResource(R.drawable.main_one);
            iv_home_liebiao.setBackgroundResource(R.drawable.main_three);
            iv_home_dextrity.setBackgroundResource(R.drawable.main_two);
            iv_home_zhanghu.setBackgroundResource(R.drawable.main_four);
        }
        switch (mTabHost.getCurrentTabTag()) {
            case TAB_TAG_SHOU:
                if (new_year()) {
                    iv_home_shouye_view.setVisibility(View.VISIBLE);
                    tv_home_shouye.setTextColor(textColor_down);
                    tv_home_shouye.setTextColor(textColor_down);
                } else if (valentine()) {
                    iv_home_shouye_view.setVisibility(View.VISIBLE);
                    tv_home_shouye.setTextColor(textColor_down);
                    tv_home_shouye.setTextColor(textColor_down);
                } else {
                    tv_home_shouye.setTextColor(textColor_down);
                }
                break;
            case TAB_TAG_Dextrity:
                if (new_year()) {
                    iv_home_dextrity_view.setVisibility(View.VISIBLE);
                    tv_home_dextrity.setTextColor(textColor_down);
                    tv_home_dextrity.setTextColor(textColor_down);
                } else if (valentine()) {
                    iv_home_dextrity_view.setVisibility(View.VISIBLE);
                    tv_home_dextrity.setTextColor(textColor_down);
                    tv_home_dextrity.setTextColor(textColor_down);
                } else {
                    tv_home_dextrity.setTextColor(textColor_down);
                }
                break;
            case TAB_TAG_LIEBIAO:
                if (new_year()) {
                    iv_home_liebiao_view.setVisibility(View.VISIBLE);
                    tv_home_liebiao.setTextColor(textColor_down);
                    tv_home_liebiao.setTextColor(textColor_down);
                } else if (valentine()) {
                    iv_home_liebiao_view.setVisibility(View.VISIBLE);
                    tv_home_liebiao.setTextColor(textColor_down);
                    tv_home_liebiao.setTextColor(textColor_down);
                } else {
                    tv_home_liebiao.setTextColor(textColor_down);
                }
                break;
            case TAB_TAG_ZHANGHU:
                if (new_year()) {
                    iv_home_zhanghu_view.setVisibility(View.VISIBLE);
                    tv_home_zhanghu.setTextColor(textColor_down);
                    tv_home_zhanghu.setTextColor(textColor_down);
                } else if (valentine()) {
                    iv_home_zhanghu_view.setVisibility(View.VISIBLE);
                    tv_home_zhanghu.setTextColor(textColor_down);
                    tv_home_zhanghu.setTextColor(textColor_down);
                } else {
                    tv_home_zhanghu.setTextColor(textColor_down);
                }
                break;

        }
    }

    private void sendToXiaoMi(String number) {
        try {
            Class miuiNotificationClass = Class.forName("android.app.MiuiNotification");
            Object miuiNotification = miuiNotificationClass.newInstance();
            Field field = miuiNotification.getClass().getDeclaredField("messageCount");
            field.setAccessible(true);
            field.set(miuiNotification, number);// 设置信息数-->这种发送必须是miui 6才行
        } catch (Exception e) {
            e.printStackTrace();
            //miui 6之前的版本
            Intent localIntent = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");
            localIntent.putExtra("android.intent.extra.update_application_component_name", getPackageName() + "/" + "11");
            localIntent.putExtra("android.intent.extra.update_application_message_text", number);
            sendBroadcast(localIntent);
        }

    }

    public Handler handler = new Handler();


    private void copyToClipBoard() {
        if (VERSION.SDK_INT < 11)
            return;
        String deviceToken = mPushAgent.getRegistrationId();
        if (!TextUtils.isEmpty(deviceToken)) {
            ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
            clipboard.setText(deviceToken);
            Toast.makeText(MainActivity.this, "DeviceToken已经复制到剪贴板了", 2000).show();
        }
    }

    private void setImmersionStatus() {
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    public static TabHost newInstance() {

        return mTabHost;
    }

    @SuppressWarnings("deprecation")
    private void initView() {
        ll_home_shouye = (LinearLayout) findViewById(R.id.ll_home_shouye);
        ll_home_liebiao = (LinearLayout) findViewById(R.id.ll_home_liebiao);
        ll_home_dextrity = (LinearLayout) findViewById(R.id.ll_home_dextrity);
        ll_home_zhanghu = (LinearLayout) findViewById(R.id.ll_home_zhanghu);
        li_bottom_tab = (AutoLinearLayout) findViewById(R.id.li_bottom_tab);
        ll_home_shouye.setOnClickListener(this);
        ll_home_liebiao.setOnClickListener(this);
        ll_home_dextrity.setOnClickListener(this);
        ll_home_zhanghu.setOnClickListener(this);
        li_activity_view = (LinearLayout) findViewById(R.id.li_activity_view);
        iv_home_liebiao_view = (ImageView) findViewById(R.id.iv_home_liebiao_view);
        iv_home_dextrity_view = (ImageView) findViewById(R.id.iv_home_dextrity_view);
        iv_home_zhanghu_view = (ImageView) findViewById(R.id.iv_home_zhanghu_view);
        iv_home_shouye_view = (ImageView) findViewById(R.id.iv_home_shouye_view);
        tv_home_shouye = (TextView) findViewById(R.id.tv_home_shouye);
        tv_home_liebiao = (TextView) findViewById(R.id.tv_home_liebiao);
        tv_home_dextrity = (TextView) findViewById(R.id.tv_home_dextrity);
        tv_home_zhanghu = (TextView) findViewById(R.id.tv_home_zhanghu);
        iv_home_shouye_activity = (ImageView) findViewById(R.id.iv_home_shouye_activity);
        iv_home_shouye = (ImageView) findViewById(R.id.iv_home_shouye);
        iv_home_liebiao = (ImageView) findViewById(R.id.iv_home_liebiao);
        iv_home_dextrity = (ImageView) findViewById(R.id.iv_home_dextrity);
        iv_home_zhanghu = (ImageView) findViewById(R.id.iv_home_zhanghu);

        mShouye = new Intent(this, HomeActivity.class);
        //我的账户
        mh5Zhanghu = new Intent(this, V6WebviewActivity.class);
        //智投宝 页面
//        mDexterity = new Intent(this, V6DexterityActivity2.class);
//        mDexterity = new Intent(this, V6DexterityActivity.class);
        mDexterity = new Intent(this, V6ZhitouIndexActivity.class);
//        mDexterity = new Intent(this, V6NewZhiTouDexterityActivity.class);
        //优享计划
        mLiebiao = new Intent(this, ProductActivity.class);
        mZichan = new Intent(this, AssetsActivity.class);
//		mZhanghu = new Intent(this, AccountActivity.class);

        mActive = new Intent(this, V4WebViewActivity.class);
        mTabHost = getTabHost();

        localTabHost = mTabHost;
        if (getIntent().getIntExtra("register", 0) == 1) {
            mh5Zhanghu.putExtra("url", Constant.URL + "h5/userInfo");
            mh5Zhanghu.putExtra("active_url", Constant.URL + "h5/userInfo?regStatus=true");
            mh5Zhanghu.putExtra("active", 1);

        }
//		else if(getIntent().getIntExtra("invest",0)==1){
//			mh5Zhanghu.putExtra("url", Constant.URL+"h5/userInfo");
//			mh5Zhanghu.putExtra("active_url", Constant.URL+"h5/userInfo"+getIntent().getStringExtra("url"));
//			mh5Zhanghu.putExtra("active",1);
//			&appversion=true
//		}
        else {
            mh5Zhanghu.putExtra("url", Constant.URL + "h5/userInfo");
            mh5Zhanghu.putExtra("active", 0);
        }
//		mActive.putExtra("url", "/landing/mValentineDay2017?app=true&music=true");
//		mActive.putExtra("title", "智投宝");
        localTabHost.addTab(buildTabSpec(TAB_TAG_SHOU, R.drawable.bbt_main_one, mShouye));
        localTabHost.addTab(buildTabSpec(TAB_TAG_Dextrity, R.drawable.bbt_main_two, mDexterity));
        localTabHost.addTab(buildTabSpec(TAB_TAG_LIEBIAO, R.drawable.bbt_main_three, mLiebiao));
        localTabHost.addTab(buildTabSpec(TAB_TAG_ZHANGHU, R.drawable.bbt_main_four, mh5Zhanghu));


        mTabHost.setOnTabChangedListener(this);

        Editor editor = sha.edit();
        editor.putString("device_token", mPushAgent.getRegistrationId());
        editor.apply();


    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
        handler = null;
        setContentView(R.layout.viewxml);
    }

    private void initData() {
        if (assets == 1) {
            moren();
            mTabHost.setCurrentTabByTag(TAB_TAG_ZHANGHU);
            iv_home_zhanghu.setSelected(true);

            iv_home_zhanghu.setBackgroundResource(R.drawable.main_four);
            tv_home_zhanghu.setTextColor(textColor_down);
//			moren();			
//			mTabHost.setCurrentTabByTag(TAB_TAG_ZICHAN);
//			iv_home_dextrity.setSelected(true);
//			tv_home_dextrity.setTextColor(textColor_down);
            assets = 0;
        } else {
            ll_home_shouye.setSelected(true);

            tv_home_shouye.setTextColor(textColor_down);
        }
        if (licai == 1 && sha.getString("page_url", "").length() <= 0) {
            moren();

            mTabHost.setCurrentTabByTag(TAB_TAG_LIEBIAO);
            iv_home_liebiao.setSelected(true);
            iv_home_liebiao.setBackgroundResource(R.drawable.main_two);
            tv_home_liebiao.setTextColor(textColor_down);
//			moren();			
//			mTabHost.setCurrentTabByTag(TAB_TAG_ZICHAN);
//			iv_home_dextrity.setSelected(true);
//			tv_home_dextrity.setTextColor(textColor_down);
            assets = 0;
        }
        if (push == 1 && sha.getString("page_url", "").length() <= 0) {
            moren();
            mTabHost.setCurrentTabByTag(TAB_TAG_LIEBIAO);
            ll_home_liebiao.setSelected(true);
            tv_home_liebiao.setTextColor(textColor_down);
            push = 0;
        }
//	if(getIntent().getIntExtra("invest",0)==1){
//		moren();			
//		mTabHost.setCurrentTabByTag(TAB_TAG_ZHANGHU);
//		ll_home_zhanghu.setSelected(true);
//		tv_home_zhanghu.setTextColor(textColor_down);
//	}
        if (getIntent().getIntExtra("register", 0) == 1) {
            moren();
            mTabHost.setCurrentTabByTag(TAB_TAG_ZHANGHU);
            ll_home_zhanghu.setSelected(true);
            tv_home_zhanghu.setTextColor(textColor_down);
        }
    }

    private boolean new_year() {
        if (Long.valueOf(ondata()) > Long.valueOf("20170122000000") && Long.valueOf(ondata()) < Long.valueOf("20170210111059")) {
            return true;
        } else {
            return false;
        }
    }

    private boolean valentine() {
        if (Long.valueOf(ondata()) > Long.valueOf("20170210111100") && Long.valueOf(ondata()) < Long.valueOf("20170215111159")) {
            return true;
        } else {
            return false;
        }
    }

    private void changeTag() {
        mTabHost.setCurrentTabByTag(TAB_TAG_LIEBIAO);
        moren();
        ll_home_liebiao.setSelected(true);
        tv_home_liebiao.setTextColor(textColor_down);
        tv_home_liebiao.getText();
    }

    private TabHost.TabSpec buildTabSpec(String tag, int resLabel,
                                         final Intent content) {
//             .setContent(i_look.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
        return mTabHost.newTabSpec(tag).setIndicator(getString(resLabel)).setContent(content.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    }

    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
//		 initView();
        tab_status(false);
        userinfo();
//		active();
        if (getIntent().getIntExtra("out", 0) == 1) {
            MainActivity.newInstance().setCurrentTabByTag("tab_tag_shouye");
        }
//		Log.e("resume", assets+"");
    }

    private void active() {
        VolleryUtil volleryUtil = new VolleryUtil() {

            @Override
            public void onUpDate(JSONObject jsonObject) {
                // TODO Auto-generated method stub
                LogUtils.e(MainActivity.this, "活动" + jsonObject.toString());
                JSONObject json = jsonObject.optJSONObject("data");
                page = json.optString("page");

//				page="";
                if (page.length() > 1) {
//					iv_home_shouye.setVisibility(View.GONE);
//		        	iv_home_shouye_activity.setVisibility(View.VISIBLE);
//		        	tv_home_shouye.setTextColor(Color.RED);
//		        	tv_home_shouye.setText("双十一专场");
                    mActive.putExtra("url", page + "?app=true&token=");
                    mActive.putExtra("title", "智投宝");


                    localTabHost.addTab(buildTabSpec(TAB_TAG_SHOU, R.drawable.bbt_main_one, mActive));
                    localTabHost.addTab(buildTabSpec(TAB_TAG_Dextrity, R.drawable.bbt_main_two, mDexterity));
                    localTabHost.addTab(buildTabSpec(TAB_TAG_LIEBIAO, R.drawable.bbt_main_two, mLiebiao));
                    localTabHost.addTab(buildTabSpec(TAB_TAG_ZHANGHU, R.drawable.bbt_main_four, mh5Zhanghu));


                } else {
                    localTabHost.removeAllViews();
                    localTabHost.addTab(buildTabSpec(TAB_TAG_SHOU, R.drawable.bbt_main_one, mShouye));
                    localTabHost.addTab(buildTabSpec(TAB_TAG_Dextrity, R.drawable.bbt_main_two, mDexterity));
                    localTabHost.addTab(buildTabSpec(TAB_TAG_LIEBIAO, R.drawable.bbt_main_two, mLiebiao));
                    localTabHost.addTab(buildTabSpec(TAB_TAG_ZHANGHU, R.drawable.bbt_main_four, mh5Zhanghu));
                }
                if (assets == 1) {
                    moren();
                    mTabHost.setCurrentTabByTag(TAB_TAG_Dextrity);
                    iv_home_dextrity.setSelected(true);

                    iv_home_dextrity.setBackgroundResource(R.drawable.main_three);
                    tv_home_dextrity.setTextColor(textColor_down);
//					moren();			
//					mTabHost.setCurrentTabByTag(TAB_TAG_ZICHAN);
//					iv_home_dextrity.setSelected(true);
//					tv_home_dextrity.setTextColor(textColor_down);
                    assets = 0;
                }
                if (licai == 1) {
                    moren();

                    mTabHost.setCurrentTabByTag(TAB_TAG_LIEBIAO);
                    iv_home_liebiao.setSelected(true);
                    iv_home_liebiao.setBackgroundResource(R.drawable.main_two);
                    tv_home_liebiao.setTextColor(textColor_down);
//					moren();			
//					mTabHost.setCurrentTabByTag(TAB_TAG_ZICHAN);
//					iv_home_dextrity.setSelected(true);
//					tv_home_dextrity.setTextColor(textColor_down);
                    assets = 0;
                }
                if (push == 1) {
                    moren();
                    mTabHost.setCurrentTabByTag(TAB_TAG_LIEBIAO);
                    ll_home_liebiao.setSelected(true);
                    tv_home_liebiao.setTextColor(textColor_down);
                    push = 0;
                }
            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub
                Log.e("失败", "失败");
                localTabHost.addTab(buildTabSpec(TAB_TAG_SHOU, R.drawable.bbt_main_one, mShouye));
                localTabHost.addTab(buildTabSpec(TAB_TAG_Dextrity, R.drawable.bbt_main_two, mDexterity));
                localTabHost.addTab(buildTabSpec(TAB_TAG_LIEBIAO, R.drawable.bbt_main_three, mLiebiao));
                localTabHost.addTab(buildTabSpec(TAB_TAG_ZHANGHU, R.drawable.bbt_main_four, mh5Zhanghu));
            }
        };

        volleryUtil.setRequestMethod(0);
        volleryUtil.setShow(true);
        volleryUtil.execute(MainActivity.this, Constant.URL + "/api/doubleEleven/jumpPage", "");
    }

    private void userinfo() {
        VolleryUtil volleryUtil = new VolleryUtil() {

            @Override
            public void onUpDate(JSONObject jsonObject) {
                Log.d(jsonObject.toString(), "3333333333333333");

                // TODO Auto-generated method stub
                if (jsonObject.optInt("success") == 1) {
                    LogUtils.e(MainActivity.this, "个人信息" + jsonObject.toString());
                    JSONObject json1 = jsonObject.optJSONObject("data");

                    Editor editor = sha.edit();
                    editor.putString("userNameText", json1.optString("userNameText"));
                    editor.putString("sex", json1.optString("sex"));
                    editor.commit();
                    if (!sha.getString("photoUrl", "").equals("")) {

                        BbtApplication.getInstance().download(sha.getString("photoUrl", ""));
                    }
//						Editor ed=sha.edit();
//						ed.putInt("islogin", 1);
//						ed.commit(); 
//					}
                } else {
                    if (jsonObject.optString("comment").equals("please login")) {
//						Editor ed=sha.edit();
//						ed.putInt("islogin", 0);
//						ed.commit();
                        Log.e("login", jsonObject.toString());
                    } else {
                        Toast.makeText(MainActivity.this, jsonObject.optString("comment"), 2000).show();
                    }
                }

            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub

            }
        };
//		volleryUtil.setHeaders("token", sha.getString("token", ""));
//		volleryUtil.setHeaders("token", sha.getString("token", ""));
// /		volleryUtil.setHeaders("token", getdevi);
//		"device_token" -> "AgW6fBRlkV_XvMF6soVTVy3KTzGK7Sn7JPC079WVu0Y-"
//		volleryUtil.setHeaders("token", sha.getString("token", ""));


//        volleryUtil.setHeaders("token", getIntent().getStringExtra("token"));
//        volleryUtil.setHeaders("token", sha.getString("token", ""));
//        volleryUtil.setHeaders("token", V6LoginActivity.token);
        Log.d("------------token", V6LoginActivity.token);
        volleryUtil.setHeaders("token", sha.getString("token", ""));
        volleryUtil.setRequestMethod(0);
        volleryUtil.execute(MainActivity.this, Constant.URL + "/api/v5/user/accountCenter", "");
    }

    public String ondata() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);
        return str;
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            long now = new Date().getTime();
            if (now - mLastBackTime < TIME_DIFF) {
//				System.exit(0);
                Editor ed = sha.edit();
                ed.putString("time", ondata());
                ed.putInt("isopen", 1);
                ed.putString("isTop", "1");
                ed.commit();
                BbtApplication.getInstance().exit();
                android.os.Process.killProcess(android.os.Process.myPid());
                //BbtApplication.getInstance().exit();

                finish();
                return super.onKeyDown(keyCode, event);
            } else {
                mLastBackTime = now;
                Toast.makeText(this, "再按一次退出优点智投", 2000).show();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void moren2() {
        iv_home_liebiao.setBackgroundResource(R.drawable.bbt_main_two);
        iv_home_shouye.setBackgroundResource(R.drawable.bbt_main_one);
        iv_home_zhanghu.setBackgroundResource(R.drawable.bbt_main_four);
        iv_home_dextrity.setBackgroundResource(R.drawable.bbt_main_three);
    }

    private void changTab(String i, int select) {
        if (i.length() < 1) {


            if (select == 1) {
//		  iv_home_shouye.setBackgroundResource(R.drawable.main_one);
                tv_home_shouye.setTextColor(textColor_down);
            } else if (select == 2) {
//		  iv_home_dextrity.setBackgroundResource(R.drawable.main_two);
                tv_home_dextrity.setTextColor(textColor_down);
            } else if (select == 3) {
//		  iv_home_liebiao.setBackgroundResource(R.drawable.main_three);
                tv_home_liebiao.setTextColor(textColor_down);
            } else if (select == 4) {
//		  iv_home_zhanghu.setBackgroundResource(R.drawable.main_four);
                tv_home_zhanghu.setTextColor(textColor_down);
            }
            Log.e("select", select + "");
        } else {
            if (select == 1) {
//			  iv_home_shouye_activity.setBackgroundResource(R.drawable.a11_11);
                tv_home_shouye.setTextColor(Color.RED);
            } else if (select == 2) {
//			  iv_home_dextrity.setBackgroundResource(R.drawable.main_two);
                tv_home_dextrity.setTextColor(textColor_down);
            } else if (select == 3) {
//			  iv_home_liebiao.setBackgroundResource(R.drawable.main_three);
                tv_home_liebiao.setTextColor(textColor_down);
            } else if (select == 4) {
//			  iv_home_zhanghu.setBackgroundResource(R.drawable.main_four);
                tv_home_zhanghu.setTextColor(textColor_down);
            }
        }

    }

    public void onClick(View v) {
        Editor ed;
        switch (v.getId()) {
            case R.id.ll_home_shouye:
                ed = sha.edit();
                ed.putInt("tab", 0);
                ed.commit();
                moren();
                mTabHost.setCurrentTabByTag(TAB_TAG_SHOU);
                iv_home_shouye.setSelected(true);
                changTab(page, 1);

//			iv_home_shouye.setBackgroundResource(R.drawable.main_one);
//			tv_home_shouye.setTextColor(textColor_down);
                break;
            case R.id.ll_home_dextrity:
                ed = sha.edit();
                ed.putInt("tab", 0);
                ed.commit();
                moren();
                mTabHost.setCurrentTabByTag(TAB_TAG_Dextrity);
                iv_home_dextrity.setSelected(true);
                changTab(page, 2);
//			iv_home_liebiao.setBackgroundResource(R.drawable.main_two);
//			tv_home_liebiao.setTextColor(textColor_down);
                break;
            case R.id.ll_home_liebiao:
                if ("false".equals(sha.getString("tabFlag", ""))) {

                    ed = sha.edit();
                    ed.putInt("tab", 0);
                    ed.commit();
                    moren();
                    mTabHost.setCurrentTabByTag(TAB_TAG_SHOU);

                    iv_home_shouye.setSelected(true);
                    changTab(page, 1);
                    return;
                }
                ed = sha.edit();
                ed.putInt("tab", 0);
                ed.commit();


                moren();
                mTabHost.setCurrentTabByTag(TAB_TAG_LIEBIAO);
                iv_home_liebiao.setSelected(true);
                changTab(page, 3);
//				iv_home_dextrity.setBackgroundResource(R.drawable.main_three);
//				tv_home_dextrity.setTextColor(textColor_down);


                break;
            case R.id.ll_home_zhanghu:
                ed = sha.edit();
                ed.putInt("tab", 0);
                ed.commit();
//			if(sha.getInt("islogin", 0)==1){

                moren();
                mTabHost.setCurrentTabByTag(TAB_TAG_ZHANGHU);
                iv_home_zhanghu.setSelected(true);
                changTab(page, 4);

                break;
        }
    }

    private void moren() {
        tv_home_shouye.setTextColor(getResources().getColor(
                R.color.bbt_new_ziti_liu));
        tv_home_liebiao.setTextColor(getResources().getColor(
                R.color.bbt_new_ziti_liu));
        tv_home_dextrity.setTextColor(getResources().getColor(
                R.color.bbt_new_ziti_liu));
        tv_home_zhanghu.setTextColor(getResources().getColor(
                R.color.bbt_new_ziti_liu));
        iv_home_liebiao.setSelected(false);
        iv_home_shouye.setSelected(false);
        iv_home_zhanghu.setSelected(false);
        iv_home_dextrity.setSelected(false);
    }

    private void deafult() {
        iv_home_shouye_view.setVisibility(View.INVISIBLE);
        iv_home_liebiao_view.setVisibility(View.INVISIBLE);
        iv_home_zhanghu_view.setVisibility(View.INVISIBLE);
        iv_home_dextrity_view.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onTabChanged(String str) {
        // TODO Auto-generated method stub
        Animation mShowAction = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha_in);
        if (str.equals("tab_tag_shouye")) {
            moren();
            iv_home_shouye.setSelected(true);
            if (li_activity_view.getVisibility() == View.VISIBLE) {
                deafult();
                iv_home_shouye_view.setVisibility(View.VISIBLE);
            }

//				iv_home_shouye_view.setAnimation(mShowAction);
//				iv_home_shouye_view.setImageResource(R.drawable.new_year_one_down);
            LocalActivityManager mlocalActivityManager = getLocalActivityManager();
            mlocalActivityManager.dispatchPause(isFinishing());
            mlocalActivityManager.dispatchResume();

            changTab(page, 1);
        } else if (str.equals("tab_tag_liebiao")) {
            LocalActivityManager mlocalActivityManager = getLocalActivityManager();
            mlocalActivityManager.dispatchPause(isFinishing());
            mlocalActivityManager.dispatchResume();

            if ("false".equals(sha.getString("tabFlag", ""))) {
                MainActivity.newInstance().setCurrentTabByTag(TAB_TAG_SHOU);
                moren();
                iv_home_shouye.setSelected(true);
                changTab(page, 1);
                return;
            }
            moren();
            iv_home_liebiao.setSelected(true);
            if (li_activity_view.getVisibility() == View.VISIBLE) {
                deafult();
                iv_home_liebiao_view.setVisibility(View.VISIBLE);
            }

//				iv_home_liebiao_view.setAnimation(mShowAction);
//				iv_home_liebiao_view.setImageResource(R.drawable.new_year_two_down);
            changTab(page, 3);
        } else if (str.equals("tab_tag_dextrity")) {
            LocalActivityManager mlocalActivityManager = getLocalActivityManager();
            mlocalActivityManager.dispatchPause(isFinishing());
            mlocalActivityManager.dispatchResume();

            moren();

            iv_home_dextrity.setSelected(true);
            if (li_activity_view.getVisibility() == View.VISIBLE) {
                deafult();
                iv_home_dextrity_view.setVisibility(View.VISIBLE);
            }


//				iv_home_dextrity_view.setAnimation(mShowAction);
//				iv_home_dextrity_view.setImageResource(R.drawable.new_year_three_down);
            changTab(page, 2);
        } else if (str.equals("tab_tag_zhanghu")) {
            LocalActivityManager mlocalActivityManager = getLocalActivityManager();
            mlocalActivityManager.dispatchPause(isFinishing());
            mlocalActivityManager.dispatchResume();

            moren();
            iv_home_zhanghu.setSelected(true);
            if (li_activity_view.getVisibility() == View.VISIBLE) {
                deafult();
                iv_home_zhanghu_view.setVisibility(View.VISIBLE);
            }

//				iv_home_zhanghu_view.setAnimation(mShowAction);
//				iv_home_zhanghu_view.setImageResource(R.drawable.new_year_four_down);
            changTab(page, 4);
        }

    }


}
